class Cache {
  constructor() {
    this.cache = new Map();
  }

  set(key, value, duration) {
    const item = {
      value,
      expiry: Date.now() + duration
    };
    this.cache.set(key, item);
  }

  get(key) {
    const item = this.cache.get(key);
    if (!item) return null;

    if (Date.now() > item.expiry) {
      this.cache.delete(key);
      return null;
    }

    return item.value;
  }

  remove(key) {
    this.cache.delete(key);
  }

  removeByPrefix(prefix) {
    for (const key of this.cache.keys()) {
      if (key.startsWith(prefix)) {
        this.cache.delete(key);
      }
    }
  }

  clear() {
    this.cache.clear();
  }
}

export default new Cache();
